package com.ayu.a.chapter03;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 徐林玉
 * @Description Recursion 递归求和
 * @Date 2022/11/3 15:24
 * @Version 1.0
 */
public class Recursion {

    public static void main(String[] args) {

        ArrayList<Integer> arr = Lists.newArrayList(1, 2, 3, 4, 5, 6);

        int sum = sum(arr);

        System.out.println("sum = " + sum);

    }


    public static int sum(ArrayList<Integer> arr){


        if(arr.size()==0){

            return 0;
        }

        if(arr.size()==1){

            return arr.get(0);
        }


        Integer first = arr.get(0);
        arr.remove(0);

        return first+sum(arr);

    }
}
